<?php
/**
 * Created by PhpStorm.
 * User: 悟能科技
 * Date: 2018/6/16
 * Time: 11:20
 */

namespace app\macht\controller;


use app\common\controller\AdminBase;
use app\common\model\PrintReceipt;

class Printset extends MachBase
{

    //列表
    public function index()
    {

        if (request()->isAjax()) {
            $PrintReceipt = new PrintReceipt();
            $param = request()->post();
            if ($this->checkmid($param['mid']) == true) {
                $data['title'] = $param['title'];
                $data['x'] = $param['x'];
                $data['y'] = $param['y'];
                $data['bmpsize1'] = $param['bmpsize1'];
                $data['bmpsize2'] = $param['bmpsize2'];
                $data['info'] = $param['info'];
                $mab['mid'] = $param['mid'];
                $info = $PrintReceipt->where($mab)->find();
                if ($info) {
                    $s = $PrintReceipt->save($data, ['mid' => $param['mid'], 'cate' => $param['cate']]);
                } else {
                    $data['mid'] = $param['mid'];
                    $data['type'] = "merchant";
                    $data['cate'] = $param['cate'];
                    $s = $PrintReceipt->save($data);
                }
                if ($s) {
                    $re_param = array('code' => 0, 'message' => "设置成功");
                } else {
                    $re_param = array('code' => 1, 'message' => "设置失败");

                }
            }else{
                $re_param = array('code' => 2, 'message' => "设置失败");
            }
            return json($re_param);

        } else {
            $PrintReceipt = new PrintReceipt();
            $map['mid'] = request()->param('id');
            if ($this->checkmid($map['mid']) == true) {
                $info = $PrintReceipt->where($map)->find();
                if (!$info) {
                    $datas = $PrintReceipt->where(array('type' => "common"))->select();
                    foreach ($datas as $k => $v) {
                        $data2[$k]['title'] = $v['title'];
                        $data2[$k]['x'] = $v['x'];
                        $data2[$k]['y'] = $v['y'];
                        $data2[$k]['bmpsize1'] = $v['bmpsize1'];
                        $data2[$k]['bmpsize2'] = $v['bmpsize2'];
                        $data2[$k]['info'] = $v['info'];
                        $data2[$k]['mid'] = request()->param('id');
                        $data2[$k]['type'] = "merchant";
                        $data2[$k]['cate'] = $v['cate'];
                    }
                    $PrintReceipt->saveAll($data2);
                }
                $infos = $PrintReceipt->where($map)->select();
                $this->assign('data', $infos);
                $this->assign('mid', request()->param('id'));
                return $this->fetch();
            }else{
                $this->error('您没有权限设置该门店！');
            }
        }

    }


    //修改
}